import "../g_vars";
import BinaClient from '../bina/BinaClient';

import storage from '../db/index';
import config from '../config';
const moment = require('moment');

import {
    timestampFormat, formatDateSub, timestampSub, formatDateAdd,formatTimeToTimestamp,timestampAdd, TRADE_TIME_FORMAT
}  from "../utils/time_fun"



import yargs  from 'yargs/yargs';
import { hideBin } from 'yargs/helpers';

const argv = yargs(hideBin(process.argv)).parse()

let symbol = "WANUSDT"
let span = "1d";
let type= "d";  // 1day d, 1 week w 1 month M

let limit = 1000;
let endTime = Date.now();
console.log(timestampFormat(endTime));

let startTime = timestampSub(endTime, 1, 'years');

console.log(timestampFormat(startTime));

let spantime = moment.duration(1000,'days');

const main = async () => {
    let binaClient = new BinaClient(false)
    await binaClient.createClient();

    while(startTime < endTime) {
        let tempEndTime = timestampAdd(startTime, limit, 'days');
        if(tempEndTime < endTime) {
            let klines = await binaClient.getKline(symbol, span, startTime, tempEndTime,limit )
            let klineModel = storage.getKlineCol(type);
            for(let i=0; i< klines.length;i++) {
                let kline = klines[i];
                console.log('kline: ', kline);

                await klineModel?.insertKline(symbol,kline);
            }
            startTime = klines[klines.length - 1][0];
        }else{
            let klines = await binaClient.getKline(symbol, span, startTime, endTime,limit )
            console.log('kline.length: ', klines.length);

            if(klines && klines.length > 0) {
                let klineModel = storage.getKlineCol(type);
                for(let i=0; i< klines.length;i++) {
                    let kline = klines[i];
                    console.log('kline: ', kline);

              //      await klineModel?.insertKline(symbol,kline);
                }

            }
            break;

        }

    }


    console.log('finish get klines!');

    process.exit(0);

}

main();